const config = require('./config/env_config');
const koa = require('koa');
const app = new koa();
const koa_router = require('koa-router')();
const json = require('koa-json');
const auth = require('./routes/auth_route.js');
const logger = require('koa-logger'); // 引入各种依赖

app.use(require('koa-bodyparser')());
app.use(json());
app.use(logger());

try {
  config.init();
} catch (error) {
  console.error(error);
}

koa_router.use('/auth', auth.routes()); // 挂载到koa-router上，同时会让所有的auth的请求路径前面加上'/auth'的请求路径。

app.use(koa_router.routes()).use(koa_router.allowedMethods()); // 将路由规则挂载到Koa上。

app.on('error', function(err, ctx){
  console.log('server error', err);
});

app.listen(8889,() => {
  console.log('Koa is listening in 8889');
});

module.exports = app;
